﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace XNAFight.Engine
{
    public class GameObject
    {
        public Vector2 position;
        private bool alive;
        
        protected SpriteManager sprite;
        protected ContentManager texture_loader;

        // A degager quand le nouveau systeme marchera
        //protected string texture_name;
        //protected Texture2D texture;
        // ------------

        // Pour le nouveau systeme
        private List<AnimationData> animationList;
        private string animationEnCours;
        // ------------


        public GameObject(ContentManager _ContentManager)
        {
            texture_loader = _ContentManager;
            position = Vector2.Zero;
            alive = false;
            
            // Nouveau systeme
            animationList = new List<AnimationData>();
        }

        public virtual void Load()
        {
            
            // Ancien systeme
            /*if (texture_name != null)
            {
                texture = texture_loader.Load<Texture2D>(texture_name);                
            }*/
            //--------------

            // Nouveau systeme
            if (AnimationList != null)
            {
                foreach (AnimationData _anim in AnimationList)
                {
                    _anim.Texture = texture_loader.Load<Texture2D>(_anim.NomTexture);
                }
            }
            // ------------
        }

        public virtual void Update(float _elapsed)
        {
            if (Sprite != null)
                Sprite.updateFrame(_elapsed);
        }

        public void draw(SpriteBatch _SpriteBatch)
        {
            sprite.draw(_SpriteBatch);
        }

        public AnimationData getAnimationData_from_AnimationName(string _name)
        {
            foreach (AnimationData _anim in animationList)
            {
                if (_anim.NomAnimation == _name)
                {
                    return _anim;
                }
            }
            return null;
        }

        public void anime(string _nomAnime)
        {
            if (_nomAnime != AnimationEnCours)
            {
                AnimationEnCours = _nomAnime;
                AnimationData nouvelleAnime = getAnimationData_from_AnimationName(_nomAnime);
                sprite.switchTexture(nouvelleAnime.Texture, nouvelleAnime.NbFrames, nouvelleAnime.WidthFrame);
                sprite.animSimple(nouvelleAnime.StartingFrame, nouvelleAnime.EndingFrame, nouvelleAnime.TempsAnime, nouvelleAnime.Loop, nouvelleAnime.SensInverse);
            }
        }

        public void anime(string _nomAnime, int _frameDepart)
        {
            if (_nomAnime != AnimationEnCours)
            {
                AnimationEnCours = _nomAnime;
                AnimationData nouvelleAnime = getAnimationData_from_AnimationName(_nomAnime);
                sprite.switchTexture(nouvelleAnime.Texture, nouvelleAnime.NbFrames, nouvelleAnime.WidthFrame);
                if(nouvelleAnime.SensInverse)
                    sprite.animSimple(nouvelleAnime.StartingFrame, _frameDepart, nouvelleAnime.TempsAnime, nouvelleAnime.Loop, nouvelleAnime.SensInverse);
                else
                    sprite.animSimple(_frameDepart, nouvelleAnime.EndingFrame, nouvelleAnime.TempsAnime, nouvelleAnime.Loop, nouvelleAnime.SensInverse);
            }
        }

        public void anime(string _nomAnime, int _frameDepart, int _frameFin, float _tempsAnime)
        {
            if (_nomAnime != AnimationEnCours)
            {
                AnimationEnCours = _nomAnime;
                AnimationData nouvelleAnime = getAnimationData_from_AnimationName(_nomAnime);
                sprite.switchTexture(nouvelleAnime.Texture, nouvelleAnime.NbFrames, nouvelleAnime.WidthFrame);
                if (nouvelleAnime.SensInverse)
                    sprite.animSimple(_frameFin, _frameDepart, _tempsAnime, nouvelleAnime.Loop, nouvelleAnime.SensInverse);
                else
                    sprite.animSimple(_frameDepart, _frameFin, _tempsAnime, nouvelleAnime.Loop, nouvelleAnime.SensInverse);
            }
        }

        #region PROPRIETES

        public bool Alive
        {
            get { return alive; }
            set { alive = value; }
        }
        public Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
                if (sprite != null)
                {
                    sprite.Position = value;
                }
            }
        }
        public SpriteManager Sprite
        {
            get { return sprite; }
        }
        // ancien systeme
        /*public string TextureName
        {
            get { return texture_name; }
        }*/

        // Nouveau systeme
        public List<AnimationData> AnimationList
        {
            get { return animationList; }
        }
        public string AnimationEnCours
        {
            get { return animationEnCours; }
            set { animationEnCours = value; }
        }

        #endregion


    }


}
